using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Data;
using System.IO;
using Evolution.Engine.Utilities;
using Microsoft.Xna.Framework.Content;
using Evolution.Utilities;

namespace Evolution.Engine.Managers
{
    public class ResourceManager : Singleton<ResourceManager>
    {
        public Hashtable _ResourceGroups = new Hashtable();
        private ResourceContent _Content;

        public ResourceContent Content { get { return _Content; } }

        public ResourceManager()
        {
            _Content = new ResourceContent(GameEngine.Instance.Service);
        }
        public object GetResource(string ResourceName, string ResourceGroup)
        {
            return FindResource(ResourceName, ResourceGroup);
        }
        public T LoadContent<T>(string resource)
        {
            return _Content.Load<T>(resource);
        }
        public void LoadResources(string resourceFile)
        {
            if (!File.Exists(GameUtils.ExecutingDirectory + resourceFile))
                throw new FileNotFoundException("Could not find resource file", resourceFile);
            LoadFile(GameUtils.ExecutingDirectory + resourceFile);
        }
        private void LoadFile(string FileName)
        {
            DataSet resourceFile = new DataSet();
            resourceFile.ReadXml(FileName);

            foreach (DataTable group in resourceFile.Tables)
            {
                foreach (DataRow resource in group.Rows)
                {
                    ImportResources(
                        group.TableName,
                        (SourceType)Enum.Parse(typeof(SourceType), resource["SourceType"].ToString()),
                        (ResourceType)Enum.Parse(typeof(ResourceType), resource["type"].ToString()),
                        GameUtils.ExecutingDirectory + resource["Path"].ToString(),
                        resource["Filter"].ToString()
                    );

                    if(((ResourceType)Enum.Parse(typeof(ResourceType), resource["type"].ToString())) == ResourceType.Quest)
                        QuestManager.Instance.LoadAllQuests(GameUtils.ExecutingDirectory + resource["Path"].ToString(), resource["Filter"].ToString());
                }
            }
        }

        private void ImportResources(string group, SourceType resourceType, ResourceType resource, string path, string filter)
        {
            if (resourceType == SourceType.Directory)
            {
                foreach (string directory in Directory.GetDirectories(path))
                    ImportResources(group, resourceType, resource, directory, filter);

                foreach (string file in Directory.GetFiles(path))
                {
                    switch (resource)
                    {
                        case ResourceType.Quest:
                            if (!_ResourceGroups.ContainsKey(group))
                            {
                                _ResourceGroups.Add(group, new Hashtable());
                            }

                            if (!((Hashtable)_ResourceGroups[group]).ContainsKey(GameUtils.FileWithoutExtention(file)))
                                ((Hashtable)_ResourceGroups[group]).Add(
                                    GameUtils.RelativeFileNameWithoutExtention(file),
                                    ProcessFile(file)
                                );
                            break;

                        case ResourceType.Script:
                            if (!_ResourceGroups.ContainsKey(group))
                            {
                                _ResourceGroups.Add(group, new Hashtable());
                            }

                            if (!((Hashtable)_ResourceGroups[group]).ContainsKey(GameUtils.FileWithoutExtention(file)))
                                ((Hashtable)_ResourceGroups[group]).Add(
                                    GameUtils.FileNameOnlyWithoutExtention(file),
                                    ProcessFile(file)
                                );
                            break;

                        case ResourceType.Level:
                            if (!_ResourceGroups.ContainsKey(group))
                            {
                                _ResourceGroups.Add(group, new Hashtable());
                            }

                            if (!((Hashtable)_ResourceGroups[group]).ContainsKey(GameUtils.FileWithoutExtention(file)))
                                ((Hashtable)_ResourceGroups[group]).Add(
                                    GameUtils.FileNameOnlyWithoutExtention(file),
                                    ProcessFile(file)
                                );
                            break;

                        case ResourceType.Shader:
                            ShaderManager.Instance.Add(new Shader(GameUtils.FileWithoutExtention(file)), GameUtils.FileNameOnlyWithoutExtention(file));
                            break;
                    }
                }

                return;
            }

            if (resourceType == SourceType.File)
            {
                if (!_ResourceGroups.ContainsKey(group))
                {
                    _ResourceGroups.Add(group, new Hashtable());
                }

                if (!((Hashtable)_ResourceGroups[group]).ContainsKey(path))
                    ((Hashtable)_ResourceGroups[group]).Add(
                        GameUtils.FileNameOnlyWithoutExtention(path),
                        ProcessFile(path)
                    );

                return;
            }
        }

        private string ProcessFile(string path)
        {
            StreamReader sr = null;
            try
            {
                sr = new StreamReader(path);
                return sr.ReadToEnd();
            }
            finally
            {
                sr.Close();
                sr = null;
            }
        }

        private object FindResource(string ResourceName, string ResourceGroup)
        {
            return ((Hashtable)_ResourceGroups[ResourceGroup])[GameUtils.FileNameOnlyWithoutExtention(ResourceName)];
        }
    }
}
